/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_heloname","_callsign","_class","_spwn","_mkrname","_asloc","_mkrname2","_stgtar","_spwndir","_helo","_gunner","_pilot","_grp","_wpAH","_arry"];

/* sets variables */
_heloname = _this select 0;
_callsign = _this select 1;
_class = _this select 2;
_spwn = _this select 3;

/* creates the marker at the editor placed location for the stationed unit */
_mkrname = _heloname + "station";
_asloc = createMarker [_mkrname, [0,0,0]];
_asloc setMarkerShape "ELLIPSE";
_mkrname setMarkerBrush "BORDER";
_mkrname setMarkerSize [100,100];
_mkrname setMarkerColor "ColorGreen";
_mkrname2 = _heloname + "station2";
_asloc2 = createMarker [_mkrname2, [0,0,0]];
_asloc2 setMarkerShape "ICON";
_mkrname2 setMarkerSize [1,1];
_mkrname2 setMarkerType "mil_Dot";
_mkrname2 setMarkerColor "ColorGreen";
_mkrname2 setMarkerText _callsign;
// _stgtar = format ["'%1' setMarkerPos _pos,'%2' setMarkerPos _pos,ASclick = true",_mkrname,_mkrname2];

/* opens map to select stationed position */
openMap [true,true];
_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;

titleText ["Select a Station Point", "PLAIN DOWN"];
RUM_ASclick = false;
waitUntil {RUM_ASclick};

_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
titleFadeOut 0;
sleep 1;

/* close map after selection */
openMap [false,false];
ASclick = false;

/* creates the unit, unit name, and positions to the onstation location */
_spwndir = getdir _spwn;
_helo = createVehicle [_class, getPos _spwn, [], 0, "FLY"];
// removeAllweapons _helo;
_helo setVehicleVarName _heloname;
_helo setpos [(getpos _spwn select 0),(getpos _spwn select 1),(getpos _spwn select 2)+RUM_AHEL_flyHeight];
_helo setdir _spwndir;

/* set the unit active and not in the standby list */
_helo setVariable ["RUM_inFlight", true, true];

/* defines the pilot and gunner */
_grp = createGroup WEST;
_pilot = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_pilot moveinDriver _helo;
_gunner = _grp createUnit [RUM_AHEL_Pilot, getpos _spwn, [], 0, "FORM"];
_gunner moveinGunner _helo;

/* removes any additional units created if no gunner position is present */
if (!(_gunner in crew _helo)) then {
	deleteVehicle _gunner;
};

/* dialog from pilot to player */
["sendtostation",[player,_pilot,_callsign]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");

/* waypoint for spawned helo for station */
_wpAH=_grp addWaypoint [getmarkerpos _mkrname2, 0];
_wpAH setWaypointType "MOVE";
_wpAH setWaypointSpeed "NORMAL";
_wpAH setWaypointBehaviour "CARELESS";
_wpAH setWaypointCombatMode "BLUE";
_wpAH setWaypointCompletionRadius 50;
_helo flyInheight RUM_AHEL_flyHeight;

sleep random(15);

/* performs a masking move to lower down near tree top level to hide while on station */
_helo doMove [(getPos _helo select 0),(getPos _helo select 1),RUM_AHEL_flyStationHeight];
_helo flyInheight RUM_AHEL_flyStationHeight;

/* waits till station move is complete */
waituntil{((getPos _helo select 2) <= (RUM_AHEL_flyStationHeight + 30))};

/* build the unit array to put back into the PDA for the stationed list */
_arry = _this + [_helo];

/* dialog from pilot to player */
["flightcheckin",[player,_pilot,_callsign]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");

/* inserts the helo into PDA station tab and makes public for all players */
RUM_AHelAircraft_onStation = RUM_AHelAircraft_onStation + [_arry];
publicVariable "RUM_AHelAircraft_onStation";

/* exits script */
if(true)exitwith{};
